<template>
    <div class="categories">
    <my-bread :breadList="breadList"></my-bread>
       <el-card>
        <el-button type="primary" @click="addCate"> 添加分类</el-button>

<tree-table
	:data="splist"
	:columns="defaultProps"
	:selection-type="false"
	:expand-type="false"
	index-text="#"
	:show-row-hover="false"
	show-index
	border
>
	<!-- 是否有效 -->
	<template slot="isok" slot-scope="scope">
		<i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted === false"></i>
    </template>
	<!-- 排序 -->
	<template slot="sorts" slot-scope="scope">
        <el-tag :type="scope.row.cat_level | color">{{scope.row.cat_level | level}}</el-tag>
	</template>
	<!-- 操作 -->
	<template slot="caozuo" slot-scope="scope">
        <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
        <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
	</template>
</tree-table>
       </el-card>
       <!-- 添加弹框 -->
       <el-dialog
  title="添加分类"
  :visible.sync="dialogAddVisible"
  width="30%">
  <el-form ref="addform" :model="addform" label-width="80px">
  <el-form-item label="分类名称">
    <el-input v-model="addform.cat_name"></el-input>
  </el-form-item>
    <el-form-item label="父级分类">
   <el-cascader
    v-model="addform.cat_pid"
    :options="splist2"
    :props="addprops"
    clearable></el-cascader>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogAddVisible = false">取 消</el-button>
    <el-button type="primary" @click="addOk">确 定</el-button>
  </span>
</el-dialog>
       <!-- 编辑弹框 -->
       <el-dialog
  title="编辑分类"
  :visible.sync="dialogEditVisible"
  width="30%">
  <el-form ref="editform" :model="editform" label-width="80px">
  <el-form-item label="分类名称">
    <el-input v-model="editform.cat_name"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogEditVisible = false">取 消</el-button>
    <el-button type="primary" @click="editOk">确 定</el-button>
  </span>
</el-dialog>
   <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
</template>

<script>
import { hqgoods, addgoods, editgoods, delgoods } from "@/http/goodsadd.js";
export default {
  data() {
    return {
      breadList:[{
      "path":"/home",
      title:"首页"
      },{
      "path":"",
      title:"商品管理"
      },{
      "path":"/categories",
      title:"商品分类"
      }],
      splist2:[],
      total:0,
     queryInfo: {
        pagesize: 5,
        pagenum: 1
      },
      index: "",
      editform: {
        cat_name: ""
      },
      dialogEditVisible: false,
      addform: {
        cat_name: "",
        cat_pid: "",
        cat_level: ""
      },
      addprops: {
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true
      },
      dialogAddVisible: false,
      splist: [],
      // data中的数据
      defaultProps: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          // 表示当前列为模板列
          type: "template",
          // 表示当前这一列的模板名称
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "sorts"
        },
        {
          label: "操作",
          type: "template",
          template: "caozuo"
        }
      ]
    };
  },
  created() {
    this.hqgoods();
  },
  methods: {
    async addCate(){
     this.dialogAddVisible=true
      let res = await hqgoods();
      // console.log(res);
      this.splist2 = res.data;
    },
     handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize=val
         this.hqgoods();
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.queryInfo.pagenum=val
         this.hqgoods();
      },
    del(row) {
      this.index = row.cat_id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let res = await delgoods(this.index);
          console.log(res);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.hqgoods();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(row) {
      // console.log(row);
      this.dialogEditVisible = true;
      this.editform.cat_name = row.cat_name;
      this.index = row.cat_id;
    },
    async editOk() {
      let res = await editgoods(this.index, {
        cat_name: this.editform.cat_name
      });
      console.log(res);
      this.dialogEditVisible = false;
      this.hqgoods();
    },
    async addOk() {
      let length = this.addform.cat_pid.length;
      let cat_pid = length === 0 ? 0 : this.addform.cat_pid[length - 1];
      let cat_level = length === 0 ? 0 : length;
      let res = await addgoods({
        cat_name: this.addform.cat_name,
        cat_pid: cat_pid,
        cat_level
      });
      console.log(res);
      this.dialogAddVisible = false;
      this.hqgoods();
    },
    async hqgoods() {
      let res = await hqgoods(this.queryInfo);
      console.log(res);
      this.total=res.data.total
      this.splist = res.data.result;
    }
  }
};
</script>
<style  lang="scss"  >
.el-cascader-panel {
  height: 300px;
}
</style>
